package ywpt.service.config;

import java.util.List;

import ywpt.core.dao.extend.HibernateEntityExtendDao;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.model.CommonTypes;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
@Service("commonTypesService")
@RemoteProxy(name="commonTypesService")
public class CommonTypesServiceImpl extends HibernateEntityExtendDao<CommonTypes> implements CommonTypesService{

	@Override
	public String createCode(String prefixCode) {
		return null;
	}

	@Override
	public CommonTypes getEntityById(Long id) {
		return this.get(id);
	}

	@Override
	public int getEntityCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public List<CommonTypes> getEntitys() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommonTypes> getEntitys(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommonTypes> getEntitys(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<CommonTypes> getEntitysByName(String name, int start, int size) {
		return null;
	}

	@Override
	public int getEntitysCountByName(String name) {
		return 0;
	}

	@Override
	public void remove(Long id) {
		CommonTypes temp=this.get(id);
		if(temp!=null)
		{
			temp.setDeleteFlag(true);
		}
		this.update(temp);
		
	}

	@Override
	public void save(CommonTypes commonTypes) {
		super.save(commonTypes);
		
	}

	@Override
	public void update(CommonTypes commonTypes) {
		this.onValid(commonTypes);
		super.update(commonTypes);
		
	}
	
	@Override
	protected void onValid(CommonTypes entity) {
		if(!isUnique(CommonTypes.class,entity,"code"))
			throw new UniqueConstraintException("�����Ѿ����ڣ���������룡");
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<CommonTypes> getCommonTypesByArea(String areaId) {
		DetachedCriteria query=DetachedCriteria.forClass(CommonTypes.class);
		if(!StringTools.isNullOrSpace(areaId)){
			query.createAlias("area", "area").add(
			        Restrictions.eq("area.id", 
			        Long.parseLong(areaId)));
		}
		query.add(Restrictions.eq("deleteFlag", false));
		return this.findAllByCriteria(query);
	}

}
